﻿
namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    using Huirui.Cavan.Core.Cryptography;
    using Huirui.Cavan.Core.Extension;
    using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
    using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
    using Huirui.Cavan.Domain.Seedwork;
    using Spring.Context;
    using Spring.Context.Support;
    using System;

    public class UserAccountAppService : BaseAppService<UserAccount, UserAccountSearch, IUserAccountRepository>, IUserAccountAppService
    {
        private IRoleAppService RoleAppService { get; set; }
        #region IUserAccountAppService Members

        public BoolResult UpdateLoginStatus(string loginName, string password, bool autoLogin, SystemIdentifier.Role role, string returnUrl)
        {
            this.SpringContext = ContextRegistry.GetContext();

            BoolResult result = new BoolResult();
            UserAccountSearch userAccountSearch = SearchFactory.CreateUserAccountSearch(loginName, SearchIdentifier.UserAccountSearchType.Name);
            UserAccount loginUserAccount = this.GetSingleBySearch(userAccountSearch);
            if (loginUserAccount != null)
            {
                if (loginUserAccount.Password != Crypto.Encrypt(password))
                {
                    result.IsSuccess = false;
                    result.Messages.Add("密码错误");
                }
                else
                {
                    if (returnUrl.IsNotEmpty())
                    {
                        result.Messages.Add(returnUrl);
                    }
                }
            }
            else
            {
                result.IsSuccess = false;
                result.Messages.Add("登录失败");
            }

            return result;
        }

        public override BoolGenericResult<UserAccount> AddOrUpdate(UserAccount entity)
        {
            Init();
            BoolGenericResult<UserAccount> result = new BoolGenericResult<UserAccount>();
            if (entity.IsNull())
            {
                throw new ArgumentNullException("");
            }
            this.RoleAppService = this.SpringContext.GetObject(SystemModuleConst.ROLEAPPSERVICE) as IRoleAppService;
            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            Administrator admin = new Administrator(employee);

            if (entity.Employee.IsNotNull() && entity.Employee.Id.IsNotEmpty())
            {
                entity.Employee = this.EmployeeAppService.GetById(entity.Employee.Id);
            }

            UserAccount oldUserAccount = this.Repository.Get(entity.Id);
            if (oldUserAccount.IsNull())
            {
                result = admin.AddUserAccount(entity.Name, entity.Password, entity.Employee, entity.IsLocked);
                if (result.IsSuccess)
                {
                    this.RoleAppService.Add(result.Object.Role);
                    result = base.Add(result.Object);
                }
            }
            else
            {
                result = admin.UpdateUserAccount(oldUserAccount, entity.Name, entity.Password, entity.IsLocked);
                if (result.IsSuccess)
                    result = base.Update(result.Object);
            }
            return result;
        }
        #endregion
    }
}
